 /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ManageUsersPanel.java
 *
 * Created on 17.Kas.2009, 14:32:12
 */

package fairhelper.panels;

import fairhelper.UserManager;
import fairhelper.language.Language;
import fairhelper.theme.Theme;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 *
 * @author Feoran
 */
public class ManageUsersPanel extends javax.swing.JPanel {

    private AddUserPanel addUserPanel;
    private ManageUsersMainPanel manageUsersMainPanel;
    private UpdateUserPanel updateUserPanel;

    /** Creates new form ManageUsersPanel */
    public ManageUsersPanel() {
        initComponents();
        manageUsersPanelManageUsersButtonActionPerformed(null);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        menuPanel = new javax.swing.JPanel();
        manageUsersPanelManageUsersButton = new javax.swing.JButton();
        addUserPanelButton = new javax.swing.JButton();
        managePanelScrollPane = new javax.swing.JScrollPane();

        setBackground(Theme.getColor("backgroundColor1"));
        setPreferredSize(new java.awt.Dimension(800, 480));

        menuPanel.setBackground(Theme.getColor("backgroundColor1"));
        menuPanel.setPreferredSize(new java.awt.Dimension(180, 480));

        manageUsersPanelManageUsersButton.setBackground(Theme.getColor("backgroundColor2"));
        manageUsersPanelManageUsersButton.setForeground(Theme.getColor("labelColor1"));
        manageUsersPanelManageUsersButton.setText(Language.getText("manageUsersPanelButton"));
        manageUsersPanelManageUsersButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                manageUsersPanelManageUsersButtonActionPerformed(evt);
            }
        });

        addUserPanelButton.setBackground(Theme.getColor("backgroundColor2"));
        addUserPanelButton.setForeground(Theme.getColor("labelColor1"));
        addUserPanelButton.setText(Language.getText("addUserPanelButton"));
        addUserPanelButton.setEnabled(userHasManageUsersPrivilege());
        addUserPanelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addUserPanelButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout menuPanelLayout = new javax.swing.GroupLayout(menuPanel);
        menuPanel.setLayout(menuPanelLayout);
        menuPanelLayout.setHorizontalGroup(
            menuPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(menuPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(menuPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(manageUsersPanelManageUsersButton, javax.swing.GroupLayout.DEFAULT_SIZE, 156, Short.MAX_VALUE)
                    .addComponent(addUserPanelButton, javax.swing.GroupLayout.DEFAULT_SIZE, 156, Short.MAX_VALUE))
                .addContainerGap())
        );
        menuPanelLayout.setVerticalGroup(
            menuPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(menuPanelLayout.createSequentialGroup()
                .addGap(116, 116, 116)
                .addComponent(manageUsersPanelManageUsersButton)
                .addGap(18, 18, 18)
                .addComponent(addUserPanelButton)
                .addContainerGap(296, Short.MAX_VALUE))
        );

        managePanelScrollPane.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(managePanelScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 614, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(menuPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(menuPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(managePanelScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 400, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void addUserPanelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addUserPanelButtonActionPerformed
        manageUsersMainPanel = null;
        addUserPanel = new AddUserPanel();
        managePanelScrollPane.setViewportView(addUserPanel);
        managePanelScrollPane.revalidate();
        managePanelScrollPane.repaint();
    }//GEN-LAST:event_addUserPanelButtonActionPerformed

    private void manageUsersPanelManageUsersButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_manageUsersPanelManageUsersButtonActionPerformed
        addUserPanel = null;
        manageUsersMainPanel = new ManageUsersMainPanel();
        managePanelScrollPane.setViewportView(manageUsersMainPanel);
        managePanelScrollPane.revalidate();
        managePanelScrollPane.repaint();
        manageUsersMainPanel.getUpdateUserPanelButton().addActionListener(new UpdateUserPanelHandler());
    }//GEN-LAST:event_manageUsersPanelManageUsersButtonActionPerformed

    private class UpdateUserPanelHandler implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            updateUserPanel = new UpdateUserPanel(UserManager.getUser(manageUsersMainPanel.getSelectedUserName()));
            managePanelScrollPane.setViewportView(updateUserPanel);
            managePanelScrollPane.revalidate();
            managePanelScrollPane.repaint();
            updateUserPanel.getBackButton().addActionListener(new BackButtonHandler());
        }

    }

    private class BackButtonHandler implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            manageUsersPanelManageUsersButtonActionPerformed(null);
        }

    }

    private boolean userHasManageUsersPrivilege() {
        if (UserManager.getPrivilegesNameArray(true).length > 0)
            return true;
        return false;
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addUserPanelButton;
    private javax.swing.JScrollPane managePanelScrollPane;
    private javax.swing.JButton manageUsersPanelManageUsersButton;
    private javax.swing.JPanel menuPanel;
    // End of variables declaration//GEN-END:variables

}
